import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { PathParam } from "../config/pathParam";

export class SystemNotice {
  id:number;
  type:string;
  fatherType:string;
  fatherId:number;
  firstId:number;
  secondId:number;
  userId:number;
  time:number;
  timeStr:string;
  content:string;  
  owner:string;
  isRead:boolean;
}

@Injectable({
  providedIn: 'root'
})
export class SystemNoticeService {
  private systemNoticeURL:string = PathParam.base_url + 'api/systemNotice/';

  constructor(private http: HttpClient) { }

  public Get(userId:number){
    let jsonObj:Object = {};
    jsonObj["userId"] = userId;
    return this.http.post(this.systemNoticeURL+'get', jsonObj);
  }

  // userId:要通知的用户
  // ownerId:造成通知的用户
  // 其他参数在后端model中有解释
  public Add(type:string, fatherType:string, fatherId:number, firstId:number, secondId:number, 
      userId:number, ownerId:number, content:string){
    let time = new Date().getTime().toString().substring(0, 10);  // 精确到秒
    let jsonObj = {"type":type, "fatherId":fatherId, "fatherType":fatherType, "firstId":firstId, "secondId":secondId, 
      "userId":userId, "ownerId":ownerId, "time":time, "content":content};
    return this.http.post(this.systemNoticeURL+'add', jsonObj, {responseType: "text"});
  }

  public Delete(id:number){
    let jsonObj = {"id":id};
    return this.http.post(this.systemNoticeURL+'delete', jsonObj, {responseType: "text"});
  }

  public Clear(userId:number){
    let jsonObj = {"userId":userId};
    return this.http.post(this.systemNoticeURL+'clear', jsonObj, {responseType: "text"});
  }

  public Read(id:number){
    let jsonObj = {"id":id};
    return this.http.post(this.systemNoticeURL+'read', jsonObj, {responseType: "text"});
  }
}
